home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / pc / Code / Chapter08 / TranslateXYZPanel.java < prev   
Text File  |  2000-06-27  |  2KB  |  108 lines

  1.  
  2. package applets;
  3.  
  4. import shout3d.*;
  5. import shout3d.core.*;
  6. import shout3d.math.*;
  7.  
  8.  
  9. public class TranslateXYZPanel extends Shout3DPanel implements DeviceObserver{
  10.     
  11.     Transform boxTrans;
  12.     float[] worldPos = new float[3];
  13.     int pixelStartX;
  14.     int pixelStartY;
  15.     int pixelEndX;
  16.     int pixelEndY;
  17.  
  18.  
  19.     public TranslateXYZPanel (Shout3DApplet applet){
  20.         super(applet);
  21.     }
  22.     
  23.         public void customInitialize() {
  24.         addDeviceObserver(this,"MouseInput", null);
  25.  
  26.         boxTrans = (Transform) getNodeByName("mytrans");
  27.  
  28.         worldPos = boxTrans.translation.getValue();
  29.  
  30.         System.out.println("Box's x position is " + worldPos[0]);
  31.         System.out.println("Box's y position is " + worldPos[1]);
  32.         System.out.println("Box's z position is " + worldPos[2]);        
  33.     }
  34.  
  35.  
  36.     protected void finalize()  { 
  37.         removeDeviceObserver(this,"MouseInput");
  38.  
  39.     }
  40.  
  41.  
  42.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  43.         MouseInput mi = (MouseInput) di;
  44.  
  45.         switch (mi.which){
  46.             case MouseInput.DOWN:
  47.                 pixelStartX = mi.x;
  48.                 pixelStartY = mi.y;
  49.                 return true;                                        
  50.  
  51.             case MouseInput.DRAG:
  52.               
  53.               //if left button used
  54.               if(mi.button == 0) {
  55.                 
  56.                 pixelEndX = mi.x;
  57.                 pixelEndY = mi.y;
  58.                 
  59.                 int dragDistanceX = pixelEndX - pixelStartX;
  60.                 int dragDistanceY = pixelEndY - pixelStartY;
  61.  
  62.                 float deltaX = dragDistanceX/50f;
  63.                 float deltaY = -(dragDistanceY/50f);
  64.                 
  65.                 worldPos[0] = worldPos[0] + deltaX;
  66.                 worldPos[1] = worldPos[1] + deltaY;
  67.  
  68.                 boxTrans.translation.setValue(worldPos);
  69.  
  70.                 pixelStartX = pixelEndX;
  71.                 pixelStartY = pixelEndY;
  72.                 
  73.                 return true;
  74.               }//end of 0 if
  75.  
  76.               //if right button used
  77.               if (mi.button == 1) {
  78.                 
  79.                 pixelEndY = mi.y;
  80.                 
  81.                 //get y pixel distances only
  82.                 int dragDistanceY = pixelEndY - pixelStartY;
  83.  
  84.                 //vertical drag converted
  85.                 //to 3D depth delta
  86.                 float deltaZ = dragDistanceY/50f;
  87.  
  88.                 //add delta to current Z
  89.                 worldPos[2] = worldPos[2] + deltaZ;
  90.  
  91.                 //put the updated position array
  92.                 //in the transform node.
  93.                 boxTrans.translation.setValue(worldPos);
  94.  
  95.                 //reset the starting pixel for next drag
  96.                 pixelStartY = pixelEndY;
  97.                 
  98.                 return true;
  99.  
  100.               }//end of 1 if
  101.  
  102.         }//end of switch
  103.  
  104.         return false;        
  105.     }
  106.  
  107.  
  108. } //end of class